import 'package:flutter/material.dart';
import 'package:flutter_fronter_service/model/address_entity.dart';
import 'package:get/get.dart';

import '../api/addressApi.dart';
import '../controller/UpdateController.dart';

class DeliveryAddressPage extends StatelessWidget {
  final UpdateController controller = Get.put(UpdateController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('地址管理'),
        centerTitle: true,
      ),
      body: Obx(() => ListView(
        children: [
          ...controller.addressList.map((item) => AddressContainer(
            addressEntity: item,
            controller: controller,
            onTap: () async {
              await controller.delete(item.id);
            },
          )).toList(),
          TextButton(onPressed: () {
            Get.toNamed("/saveAddress")!.then((value) {
              if (value == true) {
                controller.getGoodsList();
              }
            });
          }, child: const Text("新增地址"))
        ],
      )),
    );
  }
}



class AddressContainer extends StatelessWidget {
  final AddressEntity addressEntity;
  final UpdateController controller;
  //回调函数
  final VoidCallback onTap;

  const AddressContainer({
    Key? key,
    required this.addressEntity, required this.onTap, required this.controller
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        Get.back(result:addressEntity) ;
      },
      child: Container(

        height: 130,
        margin: EdgeInsets.all(16),
        padding: EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.2),
              spreadRadius: 2,
              blurRadius: 4,
              offset: Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  addressEntity.contactName,
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                if (addressEntity.defaultAddress)
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 8, vertical: 2),
                    decoration: BoxDecoration(
                      color: Colors.green,
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Text(
                      '默认',
                      style: TextStyle(color: Colors.white, fontSize: 12),
                    ),
                  ),
                Text(
                  addressEntity.contactTel,
                  style: TextStyle(color: Colors.grey[600]),
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Text(
                "${addressEntity.province}${addressEntity.city}${addressEntity.district}${addressEntity.address}",
                style: TextStyle(fontSize: 16),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                IconButton(
                  icon: Icon(Icons.edit, color: Colors.blue),
                  onPressed:(){
                    Get.toNamed("/editAddress",arguments: {"id":addressEntity.id})!.then((value) {
                      if (value == true) {
                        controller.getGoodsList();
                      }
                    });
                  },
                ),
                IconButton(
                  icon: Icon(Icons.delete, color: Colors.red),
                  onPressed:() async {
                    onTap();
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

